package net.goutalk.glcs.common.utils;


import com.aliyun.dingtalkcontact_1_0.Client;
import com.aliyun.dingtalkcontact_1_0.models.GetUserResponse;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetUserTokenResponse;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class DingtalkUtil {


    /**
     * 使用 Token 初始化账号Client
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dingtalkcontact_1_0.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkcontact_1_0.Client(config);
    }

    @SneakyThrows
    public static GetUserTokenResponse getToken(String unionId,String code){



        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        com.aliyun.dingtalkoauth2_1_0.Client client = new com.aliyun.dingtalkoauth2_1_0.Client(config);

        GetUserTokenRequest getUserTokenRequest = new GetUserTokenRequest()
                .setClientId("dingaex2gok1rllumlqs")
                .setClientSecret("Nv2pe-UoR0Z_Iw8d29laZfL3kH6ElmhZpRdPwI7SvmKJmaCI29qCrSlFEiMM88MB")
                .setCode(code)
                .setRefreshToken(code)
                .setGrantType("authorization_code");

        return client.getUserToken(getUserTokenRequest);
    }

}
